using System;
using System.Collections;
using System.Collections.Generic;
using Coffee.UIExtensions;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class RvDrillBoost : SingletonMB<RvDrillBoost>
{
	[SerializeField]
	private GameObject _button;

	[SerializeField]
	private GameObject _adsPlayImage;

	[SerializeField]
	private GameObject _buttonText;

	[SerializeField]
	private UIShiny _UIShiny;

	[SerializeField]
	private Text _timer;

	private List<TreadmillController> treadmills = new List<TreadmillController>();

	public bool bonusActive;

	private int currentTime = 30;

	public bool bonusUsed => NintendoPlayerPrefs.GetInt("BoostDrillUsed", 0) == 1;

	private void Start()
	{
		_button.SetActive(value: false);
		_adsPlayImage.SetActive(value: false);
		_timer.gameObject.SetActive(value: false);
		GameManager instance = SingletonMB<GameManager>.Instance;
		instance.OnGameStart = (Action)Delegate.Combine(instance.OnGameStart, new Action(OnGameStart));
		GameManager instance2 = SingletonMB<GameManager>.Instance;
		instance2.OnGameStart = (Action)Delegate.Combine(instance2.OnGameStart, (Action)delegate
		{
			StartCoroutine(DrillBoostTimer());
		});
		GameManager instance3 = SingletonMB<GameManager>.Instance;
		instance3.OnGameEnd = (Action)Delegate.Combine(instance3.OnGameEnd, new Action(OnGameEnd));
		treadmills.AddRange(UnityEngine.Object.FindObjectsOfType<TreadmillController>());
	}

	private void OnGameStart()
	{
		currentTime = 30;
		_UIShiny.enabled = true;
		_button.SetActive(value: false);
		_adsPlayImage.SetActive(value: false);
		_timer.gameObject.SetActive(value: false);
		_buttonText.SetActive(value: true);
		_button.GetComponent<RectTransform>().anchoredPosition = new Vector2(0f, -340f);
	}

	private void OnGameEnd()
	{
		if (bonusActive)
		{
			bonusActive = false;
			UnityEngine.Object.FindObjectOfType<Upgrade>().UpdateLong();
		}
		foreach (TreadmillController treadmill in treadmills)
		{
			if (treadmill.gameObject.activeInHierarchy)
			{
				treadmill.DrillBoosterModeFinish();
			}
		}
		bonusActive = false;
		StopAllCoroutines();
		_button.GetComponent<RectTransform>().anchoredPosition = new Vector2(-170f, -340f);
		bool flag = NintendoPlayerPrefs.GetInt("Size", 0) >= 8 || NintendoPlayerPrefs.GetInt("Power", 0) >= 14;
		_button.SetActive(value: false);
		int returnCount = SingletonMB<GameManager>.Instance.ReturnCount;
		if (Preloader.StartRVConfig.TemporaryDrillBoost && !flag && (returnCount == 3 || (returnCount > 4 && returnCount % 2 == 0)))
		{
			StartCoroutine(CheckForButton());
		}
	}

	private IEnumerator CheckForButton()
	{
		yield return null;
		_button.SetActive(value: true);
		_UIShiny.GetComponent<Button>().interactable = true;
		_adsPlayImage.SetActive(bonusUsed);
	}

	public void ButtonCliced()
	{
		ActiveBonus();
	}

	private void ActiveBonus()
	{
		bonusActive = true;
		_button.SetActive(value: false);
		NintendoPlayerPrefs.SetInt("BoostDrillUsed", 1);
		UnityEngine.Object.FindObjectOfType<Upgrade>().UpdateLong();
	}

	private IEnumerator DrillBoostTimer()
	{
		if (!bonusActive)
		{
			yield break;
		}
		foreach (TreadmillController treadmill in treadmills)
		{
			if (treadmill.gameObject.activeInHierarchy)
			{
				treadmill.DrillBoosterModeStart();
			}
		}
		_button.SetActive(value: true);
		_UIShiny.GetComponent<Button>().interactable = false;
		_buttonText.SetActive(value: false);
		_timer.gameObject.SetActive(value: true);
		UnityEngine.Object.FindObjectOfType<Control>();
		_timer.text = currentTime.ToString();
		_UIShiny.enabled = false;
		while (currentTime > 0)
		{
			yield return new WaitForSeconds(1f);
			currentTime--;
			_timer.text = currentTime.ToString();
		}
		bonusActive = false;
		_button.SetActive(value: false);
		_buttonText.SetActive(value: true);
		_timer.gameObject.SetActive(value: false);
		_UIShiny.enabled = true;
		UnityEngine.Object.FindObjectOfType<SkinApplicator>().ApplySkin();
		UnityEngine.Object.FindObjectOfType<Upgrade>().UpdateSize();
	}
}
